package jschool.manejadores;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.JOptionPane;

import jschool.entidades.Pago;
import jschool.persistencia.Conexion;

public class ManejadorDePago {
	private static ManejadorDePago instancia = null;
	private ArrayList<Pago> pagos = null;
	private Conexion conexion = null;
	private ResultSet rs = null;
	private PreparedStatement stmt = null;

	private ManejadorDePago() {
		conexion = Conexion.getInstancia();
	}

	public static ManejadorDePago getInstancia() {
		if (instancia == null) {
			instancia = new ManejadorDePago();
		}
		return instancia;
	}

	public ArrayList<Pago> getPagos() {
		pagos = new ArrayList<Pago>();
		try {
			String sql = "SELECT * FROM Pago";
			stmt = (PreparedStatement) conexion.hacerEnunciado(sql);
			rs = conexion.hacerConsulta(sql);
			while (rs.next()) {
				Pago pago = new Pago(rs.getInt(1), rs.getDouble(2), rs
						.getDouble(3), rs.getDouble(4), rs.getDouble(5), rs
						.getString(6));
				pagos.add(pago);
			}
		} catch (SQLException e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "Error Listando Pagos",
					"Error", JOptionPane.ERROR_MESSAGE);
			return null;
		}
		return pagos;
	}
	
	public int agregarPago(Pago pago){
		int fila = 0;
		try{
			String sql = "INSERT INTO Pago(idEstudiante, montoMensual, bonificacion, montoTotal, mora, fechaDePago) VALUES (?,?,?,?,?,?)";
			stmt = (PreparedStatement)conexion.hacerEnunciado(sql);
			stmt.setInt(1, pago.getIdEstudiante());
			stmt.setDouble(2, pago.getMontoMensual());
			stmt.setDouble(3, pago.getBonificacion());
			stmt.setDouble(4, pago.getMontoTotal());
			stmt.setDouble(5, pago.getMora());
			stmt.setString(6, pago.getFechaDePago());
			fila = stmt.executeUpdate();
		}catch(SQLException e){
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "Error Agregando Pago",
					"Error", JOptionPane.ERROR_MESSAGE);
			return 0;
		} finally {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return fila;
	}
	
	public int modificarPago(Pago pago, int id){
		int fila = 0;
		try{
			String sql = "UPDATE pago SET montoMensual = ?, bonificacion = ?, montoTotal = ?, mora = ?, fechaDePago = ? WHERE idEstudiante = ?";
			stmt = (PreparedStatement)conexion.hacerEnunciado(sql);
			stmt.setDouble(1, pago.getMontoMensual());
			stmt.setDouble(2, pago.getBonificacion());
			stmt.setDouble(3, pago.getMontoTotal());
			stmt.setDouble(4, pago.getMora());
			stmt.setString(5, pago.getFechaDePago());
			stmt.setInt(6, id);
		}catch(SQLException e){
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "Error Modificando Pago",
					"Error", JOptionPane.ERROR_MESSAGE);
			return 0;
		} finally {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return fila;
	}
	
	public int eliminarPago(int id){
		int fila = 0;
		try{
			String sql = "DELETE FROM Pago WHERE idEstudiante = ?";
			stmt = (PreparedStatement)conexion.hacerEnunciado(sql);
			stmt.setInt(1, id);
		}catch(SQLException e){
			e.printStackTrace();
			JOptionPane.showMessageDialog(null, "Error Eliminando Pago",
					"Error", JOptionPane.ERROR_MESSAGE);
			return 0;
		} finally {
			try {
				stmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return fila;
	}
}
